/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ipartek.formacion.gestformwww.controlador;

import com.ipartek.formacion.gestformwww.modelo.*;
import com.ipartek.formacion.gestformwww.modelo.beans.*;
import com.ipartek.formacion.gestformwww.modelo.beans.excepciones.GestorFormacionExcepcion;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 *
 * @author Jon G. & Erik F.
 */
public class VisualizarEmailAccion implements Accion{
    /*Rutas para cargar la lista candidatos o visualizar 1*/
    private final String visualizarcandidato = "visualizar-candidato-accion";
    private final String listarcandidatos = "listar-candidatos-accion";

/**
 *
 * @param servlet
 * @param request
 * @param response
 * @return
 */
    public AccionRouter ejecuta(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response) {
        HttpSession sesion;
        sesion = request.getSession();
        AccionRouter router=new AccionRouter("principal");
          AccionServlet as=(AccionServlet) servlet;
        Integer idCandidato = -1;
        Integer idConvocatoria = -1;

        ModeloCorreo emails = (ModeloCorreo) servlet.getServletContext().getAttribute("ModeloCorreo");
        ArrayList<Correo> email = new ArrayList<Correo>();

        if ((Verificador.numeroValido(request.getParameter("canId")) || (request.getAttribute("canId") != null))&&
                (Verificador.numeroValido(request.getParameter("idCon")) || (request.getAttribute("idCon") != null))){
                if (request.getAttribute("canId") != null) {
                    idCandidato = (Integer) request.getAttribute("canId");
                } else {
                    idCandidato = Integer.parseInt(request.getParameter("canId"));
                }
                if (request.getAttribute("idConvocatoria") != null) {
                    idConvocatoria = (Integer) request.getAttribute("idCon");
                } else {
                    idConvocatoria = Integer.parseInt(request.getParameter("idCon"));
                }
            try {
                email = emails.listarCorreoCandidatoConvocatoria( idCandidato,idConvocatoria);
            } catch (GestorFormacionExcepcion ex) {
                Logger.getLogger(VisualizarEmailAccion.class.getName()).log(Level.SEVERE, null, ex);
            }
                 /*cambiar por la lista de emails cuando este cargada*/
                if (email!=null){
                    request.setAttribute("listaemails", email);
                    request.setAttribute("contenido-candidato", "emails-candidato");
                }
                else{
                    if(request.getParameter("idCon")!=null){
                        request.setAttribute("idCon",request.getParameter("idCon"));
                    try {
                        router=as.getFactory().getAction(as.getNombreClaseRecurso(visualizarcandidato),servlet.getClass().getClassLoader()).ejecuta(servlet, request, response);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(VisualizarEmailAccion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InstantiationException ex) {
                        Logger.getLogger(VisualizarEmailAccion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IllegalAccessException ex) {
                        Logger.getLogger(VisualizarEmailAccion.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    }else{
                    try {
                        router=as.getFactory().getAction(as.getNombreClaseRecurso(listarcandidatos),servlet.getClass().getClassLoader()).ejecuta(servlet, request, response);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(VisualizarEmailAccion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InstantiationException ex) {
                        Logger.getLogger(VisualizarEmailAccion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IllegalAccessException ex) {
                        Logger.getLogger(VisualizarEmailAccion.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    }
                 }
        }
        else{
            try {
                router=as.getFactory().getAction(as.getNombreClaseRecurso(listarcandidatos),servlet.getClass().getClassLoader()).ejecuta(servlet, request, response);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(VisualizarEmailAccion.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(VisualizarEmailAccion.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(VisualizarEmailAccion.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return router;
    }

    public void inicializa(AccionFactory factory) {

    }

}